﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

public partial class News : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            try
            {
                var news = db.News.Where(c=>c.Status ==1).OrderByDescending(c => c.NewsID).Select(c => c).Take(3);
                Repeater1.DataSource = news;
                Repeater1.DataBind();
                int id = int.Parse(Request.QueryString["id"]);
                var details = db.News.Where(c => c.NewsID == id && c.Status == 1).SingleOrDefault();
                if( details != null)
                {
                    lblTitle_new.Text = details.NewsTitle;
                    lbl_the_by.Text = details.Author;
                    DateTime time = details.DateCreated;
                    string date_create = time.ToShortDateString();
                    lbl_date_post.Text = date_create;
                    ImgNews.ImageUrl = details.ImageUrl;
                    lblSummary.Text = details.Summary;
                    ltrContent.Text = details.NewsContent;
                }
            }
            catch {

                var ne = db.News.Where(c=>c.Status == 1).OrderByDescending(c => c.NewsID).Select(c => c).Take(3);
                Repeater1.DataSource = ne;
                Repeater1.DataBind();
                var details = db.News.Where(c=>c.Status==1).OrderByDescending(c => c.NewsID).Select(c => c).Take(1).SingleOrDefault();
                if(details != null)
                {

                    lblTitle_new.Text = details.NewsTitle;
                    lbl_the_by.Text = details.Author;
                    DateTime time = details.DateCreated;
                    string date_create = time.ToShortDateString();
                    lbl_date_post.Text = date_create;
                    ImgNews.ImageUrl = details.ImageUrl;
                    lblSummary.Text = details.Summary;
                    ltrContent.Text = details.NewsContent;
                }

            }   
        }
    }
    protected void btnGui_Click(object sender, EventArgs e)
    {
        WebClient webClient = new WebClient();
        string ip = webClient.DownloadString("http://myip.ozymo.com/");
        try
        {
            int id = int.Parse(Request.QueryString["id"]);
            Comment cm = new Comment();
            cm.NewsID = id;
            cm.FullName = txtHovaTen.Text;
            cm.Email = txtEmail.Text;
            cm.Content = txtNoidung.Text;
            cm.DateComment = DateTime.Now;
            cm.IPAddress = ip;
            cm.Status = Convert.ToBoolean(1);
            db.Comments.InsertOnSubmit(cm);
            db.SubmitChanges();
            
        }
        catch {
            var details = db.News.OrderByDescending(c => c.NewsID).Select(c => c).Take(1).SingleOrDefault();
            if (details != null)
            {
                Comment cm = new Comment();
                cm.NewsID = details.NewsID;
                cm.FullName = txtHovaTen.Text;
                cm.Email = txtEmail.Text;
                cm.Content = txtNoidung.Text;
                cm.DateComment = DateTime.Now;
                cm.IPAddress = ip;
                cm.Status = Convert.ToBoolean(1);
                db.Comments.InsertOnSubmit(cm);
                db.SubmitChanges();
            }
            
        }
        txtHovaTen.Text = null;
        txtEmail.Text = null;
        txtNoidung.Text = null;
    }
}